package cn.hairui.fastms.base.config;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * <h2>全局错误页面配置</h2>
 *  <ol>请添加功能描述</ol>
 * <h2>修改历史</h2>
 *  <ol>如有修改，请添加修改历史</ol>
 * </p>
 *
 * @author lihairui
 * @date 2020/11/11 17:24
 * @copyright 李海瑞
 */
@Controller
public class ErrorConfig implements ErrorController {

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request, Model model) {
        //获取statusCode:401,404,500
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == 401) {
            return "pages/error/401";
        } else if (statusCode == 404) {
            return "pages/error/404";
        } else if (statusCode == 403) {
            return "pages/error/403";
        } else if (statusCode == 500) {
            return "pages/error/500";
        } else {
            return "pages/error/unknown";
        }
    }

    @RequestMapping("/error/403")
    public String e403() {
        return "error/403";
    }

    @RequestMapping("/error/404")
    public String e404() {
        return "error/404";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}